# -*- coding:GBK -*-

import iworld2d
import math3d
import math

class CBrickLine(object):
    def __init__(self):
        self.sp2list = []
        self.sp2num = -1
        
    def drawLine(self,start_pos,end_pos,lenstep,sp2dir):
        startpos = list(start_pos)
        endpos = list(end_pos)
        
        # calc pointcount
        vec = math3d.vector2(endpos[0]-startpos[0],endpos[1]-startpos[1])
        pointcount = math.floor(vec.length/lenstep)
        pointcount = int(pointcount)
        
        xstep = 1.0*(endpos[0]-startpos[0])/pointcount
        ystep = 1.0*(endpos[1]-startpos[1])/pointcount
        
        poslist = []
        for i in range(pointcount):
            xpos = startpos[0]+xstep*i
            ypos = startpos[1]+ystep*i
            poslist.append((xpos,ypos))
        
        # draw line
        for pos in poslist:
            sp2 = iworld2d.sprite2d(sp2dir)
            sp2.key_point = (0,0)
            sp2.pos = pos
            paralist = []
            paralist.append(sp2)
            framecount = sp2.get_frame_count()-1
            sp2.register_keyframe_event(framecount, self.destroy_sp2, paralist)
            self.sp2list.append(sp2)
            sp2.play()       
        
    def destroy_sp2(self, obj, data):
        sp2 = data[0]
        self.sp2list.remove(sp2)
        sp2.hide()
        sp2.destroy()
        sp2 = None
        self.sp2num = len(self.sp2list)